<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>执行外部程序，并且显示输出</title>
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-base.css" />
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-medium.css" />

 </head>
 <body class="docs"><div class="navbar navbar-fixed-top">
  <div class="navbar-inner clearfix">
    <ul class="nav" style="width: 100%">
      <li style="float: left;"><a href="function.shell-exec.html">« shell_exec</a></li>
      <li style="float: right;"><a href="book.parallel.html">parallel »</a></li>
    </ul>
  </div>
</div>
<div id="breadcrumbs" class="clearfix">
  <ul class="breadcrumbs-container">
    <li><a href="index.html">PHP Manual</a></li>
    <li><a href="ref.exec.html">程序执行函数</a></li>
    <li>执行外部程序，并且显示输出</li>
  </ul>
</div>
<div id="layout">
  <div id="layout-content"><div id="function.system" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">system</h1>
  <p class="verinfo">(PHP 4, PHP 5, PHP 7, PHP 8)</p><p class="refpurpose"><span class="refname">system</span> &mdash; <span class="dc-title">执行外部程序，并且显示输出</span></p>

 </div>

 <div class="refsect1 description" id="refsect1-function.system-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="methodname"><strong>system</strong></span>(<span class="methodparam"><span class="type">string</span> <code class="parameter">$command</code></span>, <span class="methodparam"><span class="type">int</span> <code class="parameter reference">&$result_code</code><span class="initializer"> = <strong><code>null</code></strong></span></span>): <span class="type"><span class="type">string</span>|<span class="type"><span class="type false">false</span></span></span></div>

  <p class="para rdfs-comment">
    同 C 版本的 <span class="function"><strong>system()</strong></span> 函数一样，本函数执行
    <code class="parameter">command</code> 参数所指定的命令，并且输出执行结果。
  </p>
  <p class="para">
    如果 PHP 运行在服务器模块中，<span class="function"><strong>system()</strong></span>
    函数还会尝试在每行输出完毕之后，自动刷新 web 服务器的输出缓存。
  </p>
  <p class="para">
    如果要获取一个命令未经任何处理的原始输出，请使用
    <span class="function"><a href="function.passthru.html" class="function">passthru()</a></span> 函数。
  </p>
 </div>


 <div class="refsect1 parameters" id="refsect1-function.system-parameters">
  <h3 class="title">参数</h3>
  <p class="para">
   <dl>
    
     <dt>
<code class="parameter">command</code></dt>

     <dd>

      <p class="para">
       要执行的命令。
      </p>
     </dd>

    
    
     <dt>
<code class="parameter">result_code</code></dt>

     <dd>

      <p class="para">
        如果提供 <code class="parameter">result_code</code>
        参数，则外部命令执行后的返回状态将会被设置到此变量中。
      </p>
     </dd>

    
   </dl>

  </p>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-function.system-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
    成功则返回命令输出的最后一行，失败则返回 <strong><code>false</code></strong>
  </p>
 </div>


 <div class="refsect1 examples" id="refsect1-function.system-examples">
  <h3 class="title">范例</h3>
  <p class="para">
   <div class="example" id="example-3313">
    <p><strong>示例 #1 <span class="function"><strong>system()</strong></span> 示例</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">echo&nbsp;</span><span style="color: #DD0000">'&lt;pre&gt;'</span><span style="color: #007700">;<br /><br /></span><span style="color: #FF8000">//&nbsp;输出&nbsp;shell&nbsp;命令&nbsp;"ls"&nbsp;的返回结果<br />//&nbsp;并且将输出的最后一样内容返回到&nbsp;$last_line。<br />//&nbsp;将命令的返回值保存到&nbsp;$retval。<br /></span><span style="color: #0000BB">$last_line&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">system</span><span style="color: #007700">(</span><span style="color: #DD0000">'ls'</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$retval</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">//&nbsp;打印更多信息<br /></span><span style="color: #007700">echo&nbsp;</span><span style="color: #DD0000">'<br />&lt;/pre&gt;<br />&lt;hr&nbsp;/&gt;Last&nbsp;line&nbsp;of&nbsp;the&nbsp;output:&nbsp;'&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #0000BB">$last_line&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #DD0000">'<br />&lt;hr&nbsp;/&gt;Return&nbsp;value:&nbsp;'&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #0000BB">$retval</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

   </div>
  </p>
 </div>


 <div class="refsect1 notes" id="refsect1-function.system-notes">
  <h3 class="title">注释</h3>
  <div class="warning"><strong class="warning">警告</strong><p class="para">当传入用户提供的数据到本函数时，应使用
<span class="function"><a href="function.escapeshellarg.html" class="function">escapeshellarg()</a></span> 或 <span class="function"><a href="function.escapeshellcmd.html" class="function">escapeshellcmd()</a></span>
来防止用户欺骗系统执行任意命令。</p></div>
  <blockquote class="note"><p><strong class="note">注意</strong>: <p class="para">如何程序使用此函数启动，为了能保持在后台运行，此程序必须将输出重定向到文件或其它输出流。否则会导致
PHP 挂起，直至程序执行结束。</p></p></blockquote>
 </div>


 <div class="refsect1 seealso" id="refsect1-function.system-seealso">
  <h3 class="title">参见</h3>
  <p class="para">
   <ul class="simplelist">
    <li class="member"><span class="function"><a href="function.exec.html" class="function" rel="rdfs-seeAlso">exec()</a> - 执行一个外部程序</span></li>
    <li class="member"><span class="function"><a href="function.passthru.html" class="function" rel="rdfs-seeAlso">passthru()</a> - 执行外部程序并且显示原始输出</span></li>
    <li class="member"><span class="function"><a href="function.popen.html" class="function" rel="rdfs-seeAlso">popen()</a> - 打开进程文件指针</span></li>
    <li class="member"><span class="function"><a href="function.escapeshellcmd.html" class="function" rel="rdfs-seeAlso">escapeshellcmd()</a> - shell 元字符转义</span></li>
    <li class="member"><span class="function"><a href="function.pcntl-exec.html" class="function" rel="rdfs-seeAlso">pcntl_exec()</a> - 在当前进程空间执行指定程序</span></li>
    <li class="member"><a href="language.operators.execution.html" class="link">执行运算符</a></li>
   </ul>
  </p>
 </div>

</div></div></div></body></html>